/*
 * Copyright (C) 2012 Trilarion
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.anote;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.WindowConstants;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.tools.ui.helper.LookAndFeel;

/**
 *
 * @author Trilarion 2012
 */
public class TextPaneTest extends JFrame {
    private static final long serialVersionUID = 1L;

    /**
     * Creates new form TextPaneTest
     */
    public TextPaneTest() {
        initComponents();
        textPane.setContentType("text/html"); // or any other styled content type
        textPane.setText("White text on a red background");
        textPane.setBackground(Color.blue);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollPane = new JScrollPane();
        textPane = new JTextPane();
        linkButton = new JButton();
        bgButton = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("Text Pane Test");

        scrollPane.setViewportView(textPane);

        linkButton.setText("Add Link");

        bgButton.setText("Set Background");
        bgButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                bgButtonActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.LEADING)
                    .addComponent(scrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(linkButton)
                        .addPreferredGap(ComponentPlacement.RELATED)
                        .addComponent(bgButton)
                        .addGap(0, 539, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.BASELINE)
                    .addComponent(linkButton)
                    .addComponent(bgButton))
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bgButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_bgButtonActionPerformed
        ColorChooserDlg dlg = new ColorChooserDlg(this);
        dlg.setVisible(true);

        Color backgroundColor = Color.red;
        SimpleAttributeSet background = new SimpleAttributeSet();
        StyleConstants.setBackground(background, backgroundColor);
        textPane.getStyledDocument().setParagraphAttributes(0, textPane.getDocument().getLength(), background, false);

        // And remove default (white) margin
        // textPane.setBorder(BorderFactory.createEmptyBorder());

        // Alternative: Leave a 2px border but draw it in the same color
        // textPane.setBorder(BorderFactory.createLineBorder(backgroundColor, 2));
    }//GEN-LAST:event_bgButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        LookAndFeel.setSystemLookAndFeel();

        /*
         * Create and display the form
         */
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TextPaneTest().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton bgButton;
    private JButton linkButton;
    private JScrollPane scrollPane;
    private JTextPane textPane;
    // End of variables declaration//GEN-END:variables
}
